package com.paic.mhis.hcpms.feedback.biz.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.common.base.dto.PaginationDTO;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.AuditWorkFlowService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dao.AuditOpinionDao;
import com.paic.mhis.hcpms.audit.dto.AuditCaseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.mhis.hcpms.feedback.biz.service.AuditFeedbackService;
import com.paic.mhis.hcpms.feedback.exception.AuditFeedbackException;
import com.paic.mhis.hcpms.feedback.intergration.dao.AuditFeedbackDAO;

/**
 * 
 * @author EX-XIEQI001
 * 
 */
@Service("auditFeedbackService")
public class AuditFeedbackServiceImpl implements AuditFeedbackService {

	@Autowired
	private CommonDAO commonDAO;

	@Autowired
	private AuditFeedbackDAO auditFeedbackDAO;

	@Autowired
	private AuditWorkFlowService auditWorkFlowService; // 流程服务
	@Autowired
	private AuditOpinionDao auditOpinionDao;
	
	@Override
	public Map<String, Object> queryAuditFeedbackAll(PaginationDTO pdto, String userId) throws AuditFeedbackException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String targetCode = auditFeedbackDAO.queryHospitalByUser(userId);
			if (StringUtils.isBlank(targetCode) || StringUtils.isEmpty(targetCode)) {
				throw new AuditFeedbackException("用户没有分配所属机构,请联系上级管理进行分配机构！！！");
			}
			Map<Object, Object> map = pdto.getQueryParams();
			map.put("targetCode", targetCode);

			pdto.setQueryParams(map);

			resultMap = commonDAO.queryPage("auditFeedback.queryCaseAll", pdto);
			resultMap.put(AuditConstants.STATE_KEY, AuditConstants.SUCCESS_STATE_VALUE);
			resultMap.put(AuditConstants.MSG_KEY, AuditConstants.SELECT_SUCCESS);
		} catch (AuditFeedbackException e) {
			throw e;
		} catch (Exception e) {
			throw new AuditFeedbackException("查询数据出错,请联系管理员！！！");
		}
		return resultMap;
	}

	@Transactional
	@Override
	public void makeAuditFeedback(String caseCode, UserDetails user) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		AuditCaseDTO dto = auditFeedbackDAO.queryAuditCase(caseCode);

		Date currentDate = new Date();

		if (dto == null) {
			throw new AuditFeedbackException("数据库中没有该案件编号,请刷新或联系管理员!!!");
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(sdf.format(currentDate));
		if (StringUtils.isNotBlank(dto.getAffirmDeadline()) && StringUtils.isNotEmpty(dto.getAffirmDeadline())) {
			Date affirmDeadline = sdf.parse(dto.getAffirmDeadline());
			if (affirmDeadline.compareTo(date) < 0) {
				throw new AuditFeedbackException("案件超过确认截止时间,不能进行操作!!!");
			}
		}
		String currentDateStr = sdf.format(currentDate);
		map.put("caseCode", caseCode);
		map.put("caseState", AuditConstants.auditCaseStatus_5);
		map.put("updatedBy", user.getUsername());
		map.put("affirmDate", currentDateStr);
		map.put("complaintFlag", AuditConstants.complaintFlag_0);
		auditFeedbackDAO.updateAuditCase(map);

		
		AuditOpinionDTO tempOpinionDTO = new AuditOpinionDTO();
		tempOpinionDTO.setProblemNo(caseCode);
		tempOpinionDTO.setOpinionType(AuditConstants.opinionType_14);
		tempOpinionDTO.setTaskType(AuditConstants.taskType_02);
		tempOpinionDTO.setIsDeleted(AuditConstants.isDeleted);
		tempOpinionDTO.setDealUserName(user.getUsername());
		auditOpinionDao.addOpinion(tempOpinionDTO);
		
		// 处理流程
		Map<String, String> condition = new HashMap<String, String>();
		condition.put(AuditConstants.AUDIT_CASE_AFFIRM_TYPE, AuditConstants.AUDIT_CASE_AFFIRM_TYPE_01);
		this.auditWorkFlowService.processWorkFlow(caseCode, user, condition, AuditConstants.TASK_TYPE_CASE);

	}

	@Transactional
	@Override
	public void appealAuditFeedback(String caseCode, UserDetails user) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		AuditCaseDTO dto = auditFeedbackDAO.queryAuditCase(caseCode);
		if (dto == null) {
			throw new AuditFeedbackException("数据库中没有该案件编号,请刷新或联系管理员!!!");
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(sdf.format(new Date()));
		if (StringUtils.isNotBlank(dto.getAffirmDeadline()) && StringUtils.isNotEmpty(dto.getAffirmDeadline())) {
			Date affirmDeadline = sdf.parse(dto.getAffirmDeadline());
			if (affirmDeadline.compareTo(date) < 0) {
				throw new AuditFeedbackException("案件超过确认截止时间,不能进行操作!!!");
			}
		}
		map.put("caseCode", caseCode);
		map.put("caseState", AuditConstants.auditCaseStatus_2);
		map.put("complaintFlag", AuditConstants.complaintFlag);
		map.put("updatedBy", user.getUsername());

		auditFeedbackDAO.updateAuditCase(map);
		
		AuditOpinionDTO tempOpinionDTO = new AuditOpinionDTO();
		tempOpinionDTO.setProblemNo(caseCode);
		tempOpinionDTO.setOpinionType(AuditConstants.opinionType_14);
		tempOpinionDTO.setTaskType(AuditConstants.taskType_02);
		tempOpinionDTO.setIsDeleted(AuditConstants.isDeleted);
		tempOpinionDTO.setDealUserName(user.getUsername());
		auditOpinionDao.addOpinion(tempOpinionDTO);
		// 处理流程
		Map<String, String> condition = new HashMap<String, String>();
		condition.put(AuditConstants.AUDIT_CASE_AFFIRM_TYPE, AuditConstants.AUDIT_CASE_AFFIRM_TYPE_02);
		this.auditWorkFlowService.processWorkFlow(caseCode, user, condition, AuditConstants.TASK_TYPE_CASE);

	}

}
